<template>
  <div class="admin-layout">
    <!-- 左侧菜单 -->
    <div class="left-menu">
      <!-- 顶部Logo -->
      <div class="logo-area">
        <div class="logo-icon">
          <!-- 口红SVG图标 -->
          <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="lipstick-logo">
            <!-- 口红底座 -->
            <rect x="5" y="16" width="14" height="6" rx="1" fill="#3a3a3a"/>
            <!-- 口红管身 -->
            <rect x="7" y="8" width="10" height="8" rx="1" fill="#e83e8c"/>
            <!-- 口红膏体 -->
            <path d="M12 2L16 8H8L12 2Z" fill="#ff6b9b"/>
            <!-- 口红边缘高光 -->
            <path d="M12 2L13 8H11L12 2Z" fill="#ff96b9"/>
            <!-- 旋转机构 -->
            <rect x="9" y="16" width="6" height="1" fill="#ffffff" opacity="0.3"/>
          </svg>
        </div>
        <div class="logo-text">美妆商城</div>
        <div class="merchant-tag">商家</div>
      </div>
      
      <!-- 菜单项 -->
      <div class="menu-list">
        <router-link to="/merchant/dashboard" class="menu-item" :class="{ 'active': isActive('/merchant/dashboard') }">
          <el-icon><Odometer /></el-icon>
          <span class="menu-text">仪表盘</span>
        </router-link>
        
        <router-link to="/merchant/orders" class="menu-item" :class="{ 'active': isActive('/merchant/orders') }">
          <el-icon><ShoppingCart /></el-icon>
          <span class="menu-text">订单管理</span>
        </router-link>
        
        <router-link to="/merchant/products" class="menu-item" :class="{ 'active': isActive('/merchant/products') }">
          <el-icon><Goods /></el-icon>
          <span class="menu-text">商品管理</span>
        </router-link>
        
        <router-link to="/merchant/inventory" class="menu-item" :class="{ 'active': isActive('/merchant/inventory') }">
          <el-icon><Box /></el-icon>
          <span class="menu-text">库存管理</span>
        </router-link>
        
        <router-link to="/merchant/logistics" class="menu-item" :class="{ 'active': isActive('/merchant/logistics') }">
          <el-icon><Van /></el-icon>
          <span class="menu-text">物流管理</span>
        </router-link>
        
        <router-link to="/merchant/analytics" class="menu-item" :class="{ 'active': isActive('/merchant/analytics') }">
          <el-icon><DataAnalysis /></el-icon>
          <span class="menu-text">销售分析</span>
        </router-link>
        
        <router-link to="/merchant/settings" class="menu-item" :class="{ 'active': isActive('/merchant/settings') }">
          <el-icon><Setting /></el-icon>
          <span class="menu-text">店铺设置</span>
        </router-link>
      </div>
      
      <div class="menu-footer">
        <div class="version">美妆商城 v1.0</div>
      </div>
    </div>
    
    <!-- 右侧内容区 -->
    <div class="right-content">
      <!-- 顶部导航栏 -->
      <div class="top-header">
        <!-- 面包屑导航 -->
        <div class="breadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>商家中心</el-breadcrumb-item>
            <el-breadcrumb-item>{{ getCurrentTitle() }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <!-- 右侧功能区 -->
        <div class="header-right">
          <!-- 通知图标 -->
          <div class="notification-icon">
            <el-badge :value="3" class="notification-badge">
              <el-icon><Bell /></el-icon>
            </el-badge>
          </div>
          
          <!-- 用户信息和下拉菜单 -->
          <div class="user-dropdown" @click="showDropdown = !showDropdown">
            <el-avatar :size="32" :src="userAvatar" class="avatar" />
            <span class="username">{{ username }}</span>
            <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
            
            <!-- 下拉菜单 -->
            <div class="dropdown-menu" v-show="showDropdown">
              <div class="dropdown-item" @click="goToProfile">
                <el-icon><User /></el-icon>
                <span>个人资料</span>
              </div>
              <div class="dropdown-item" @click="goToSettings">
                <el-icon><Setting /></el-icon>
                <span>账户设置</span>
              </div>
              <div class="dropdown-divider"></div>
              <div class="dropdown-item logout" @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>
                <span>退出登录</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 主内容区 -->
      <div class="main-content">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '../stores/user';
import { ElMessageBox, ElMessage } from 'element-plus';
import { 
  Odometer, ShoppingCart, Goods, Box, Van, DataAnalysis, 
  Setting, User, Bell, ArrowDown, SwitchButton
} from '@element-plus/icons-vue';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

// 用户信息
const username = ref(userStore.user?.username || '商家用户');
const userAvatar = ref('https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png');

// 下拉菜单控制
const showDropdown = ref(false);

// 判断当前路由是否激活
const isActive = (path: string) => {
  return route.path.includes(path);
};

// 获取当前页面标题
const getCurrentTitle = () => {
  const path = route.path;
  if (path.includes('/dashboard')) return '仪表盘';
  if (path.includes('/orders')) return '订单管理';
  if (path.includes('/products')) return '商品管理';
  if (path.includes('/inventory')) return '库存管理';
  if (path.includes('/logistics')) return '物流管理';
  if (path.includes('/analytics')) return '销售分析';
  if (path.includes('/settings')) return '店铺设置';
  return '商家中心';
};

// 导航函数
const goToProfile = () => {
  router.push('/merchant/profile');
  showDropdown.value = false;
};

const goToSettings = () => {
  router.push('/merchant/settings');
  showDropdown.value = false;
};

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    userStore.logout();
    ElMessage.success('已安全退出登录');
    router.push('/merchant/login');
  }).catch(() => {
    // 用户取消退出
  });
  
  showDropdown.value = false;
};

// 点击其他地方关闭下拉菜单
const closeDropdown = (e: MouseEvent) => {
  const dropdown = document.querySelector('.user-dropdown');
  if (dropdown && !dropdown.contains(e.target as Node)) {
    showDropdown.value = false;
  }
};

// 添加和移除全局点击事件监听
onMounted(() => {
  document.addEventListener('click', closeDropdown);
});

onUnmounted(() => {
  document.removeEventListener('click', closeDropdown);
});
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
}

.admin-layout {
  display: flex;
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}

/* 左侧菜单样式 - 全新设计 */
.left-menu {
  width: 220px;
  height: 100%;
  background: #212529;
  color: white;
  flex-shrink: 0;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1000;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
  transition: all 0.3s;
  display: flex;
  flex-direction: column;
}

.logo-area {
  height: 70px;
  background: rgba(232, 62, 140, 0.15);
  display: flex;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.logo-icon {
  width: 36px;
  height: 36px;
  margin-right: 12px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
  transform: rotate(-15deg); /* 让口红稍微倾斜一点，更有活力 */
}

.lipstick-logo {
  width: 100%;
  height: 100%;
}

.logo-text {
  font-size: 18px;
  color: white;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.merchant-tag {
  margin-left: 8px;
  background: #e83e8c;
  color: #fff;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 500;
}

.menu-list {
  padding: 12px 0;
  flex: 1;
  overflow-y: auto;
}

.menu-item {
  display: flex;
  align-items: center;
  height: 50px;
  padding: 0 20px;
  color: rgba(255, 255, 255, 0.75);
  text-decoration: none;
  cursor: pointer;
  transition: all 0.25s ease;
  margin-bottom: 2px;
  border-left: 3px solid transparent;
}

.menu-item:hover {
  background-color: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.menu-item.active {
  background: rgba(232, 62, 140, 0.15);
  color: #e83e8c;
  border-left: 3px solid #e83e8c;
}

.menu-item .el-icon {
  margin-right: 12px;
  font-size: 18px;
}

.menu-text {
  font-size: 14px;
}

.menu-footer {
  padding: 12px 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.4);
  font-size: 12px;
  text-align: center;
}

/* 右侧内容区样式 - 新设计 */
.right-content {
  flex: 1;
  margin-left: 220px;
  width: calc(100% - 220px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f8f9fa;
}

/* 顶部导航栏 - 新设计 */
.top-header {
  height: 70px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  background-color: #FFF;
  border-bottom: 1px solid #eaeaea;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.breadcrumb {
  font-size: 14px;
  color: #606266;
}

/* 右侧功能区 - 新设计 */
.header-right {
  display: flex;
  align-items: center;
}

.notification-icon {
  position: relative;
  margin-right: 24px;
  cursor: pointer;
  color: #606266;
  font-size: 20px;
}

.notification-icon:hover {
  color: #e83e8c;
}

.notification-badge :deep(.el-badge__content) {
  background-color: #e83e8c;
}

/* 用户下拉菜单 - 新设计 */
.user-dropdown {
  position: relative;
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.25s;
}

.user-dropdown:hover {
  background-color: #f5f7fa;
}

.avatar {
  margin-right: 8px;
  border: 2px solid #f5f5f5;
}

.username {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
  margin-right: 4px;
}

.dropdown-icon {
  font-size: 12px;
  color: #909399;
}

.dropdown-menu {
  position: absolute;
  top: 45px;
  right: 0;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 10;
  min-width: 150px;
  padding: 4px 0;
  border: 1px solid #eaeaea;
  transform-origin: top right;
  animation: dropdown 0.2s cubic-bezier(0.23, 1, 0.32, 1);
}

@keyframes dropdown {
  from {
    opacity: 0;
    transform: scale(0.8);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.dropdown-item {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: all 0.2s;
}

.dropdown-item:hover {
  background-color: #f5f7fa;
  color: #e83e8c;
}

.dropdown-item .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

.dropdown-divider {
  height: 1px;
  background-color: #eaeaea;
  margin: 4px 0;
}

.dropdown-item.logout {
  color: #fa5555;
}

.dropdown-item.logout:hover {
  background-color: #fef0f0;
}

/* 主内容区 - 新设计 */
.main-content {
  flex: 1;
  overflow-y: auto;
  width: 100%;
  height: calc(100vh - 70px);
  padding: 24px;
}

/* 滚动条样式 - 新设计 */
.menu-list::-webkit-scrollbar,
.main-content::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

.menu-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 10px;
}

.menu-list::-webkit-scrollbar-track {
  background: transparent;
}

.main-content::-webkit-scrollbar-thumb {
  background: #dcdfe6;
  border-radius: 10px;
}

.main-content::-webkit-scrollbar-track {
  background: transparent;
}

/* 响应式处理 - 新设计 */
@media (max-width: 768px) {
  .left-menu {
    width: 60px;
  }
  
  .right-content {
    margin-left: 60px;
    width: calc(100% - 60px);
  }
  
  .logo-text, .merchant-tag, .menu-text {
    display: none;
  }
  
  .menu-item {
    justify-content: center;
    padding: 0;
  }
  
  .menu-item .el-icon {
    margin-right: 0;
    font-size: 20px;
  }
  
  .menu-footer {
    display: none;
  }
}
</style> 
